<template>
  <div>
    <FormPanel
      ref="formPanel"
      :list="formList"
      :cols="3"
      :formType="type"
      labelWidth="90"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">返 回</el-button>
      <el-button @click="saveDialog" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { updateCustomerOne, addCustomerOne } from '@/api/retail/salesmanage/salesordermanage';
import { dictionary } from '@/utils/dictMixin';
import { notifyAction } from '@/utils';
import { IDNumber, phone, email, formatDates, formatDat, checkPassport} from '@/utils/validate'
import moment from 'moment'

export default {
  name: "NewCustomer",
  mixins: [dictionary],
  props: {
    type: '',
    rowData: {}
  },
  data () {
    return {
      formList: this.createFormList()
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    init () {
      let { type, rowData } = this
      console.log(rowData)
      if (type == 'add') {

      } else if (type == 'edit') {
        if (rowData['customerType'] == 15231001) {
          this.formList = this.createFormList(true)
        } else if (rowData['customerType'] == 15231003) {
          this.formList = this.createFormList(false)
        }
        this.formList.forEach(item => {
          if (item.fieldName == 'birthday') {
            item.initialValue = rowData['birthday']
            // item.initialValue = this.preDateHandler(rowData['birthday'],'YYYY-MM-DD')
          } else {
            item.initialValue = rowData[item.fieldName]
          }
        })
      }
    },
    createFormList (isPersonal = true) {
      // 个人/公司
      let part1 = [{
        type: 'SELECT',
        label: '客户类型',
        fieldName: 'customerType',
        placeholder: '',
        initialValue: isPersonal ? 15231001 : 15231003,
        itemList: this.createDictList('1523'),
        change: (e) => {
          let flag = e == 15231001 ? true : false
          this.formList = this.createFormList(flag)
        }
      },
      {
        type: 'INPUT',
        label: '开票名称',
        fieldName: 'customerName',
        placeholder: '',
        initialValue: '',
        rules: [{ required: true, message: '请填写...', trigger: 'blur'}],
      }]
      // 个人
      let part2 = [{
        type: 'INPUT',
        label: '开票手机',
        fieldName: 'mobilePhone',
        placeholder: '',
        initialValue: '',
        rules: [{ required: true, message: '请填写...', trigger: 'blur'},
        { required: true, pattern: /^[1]([3-9])[0-9]{9}$/, message: '请输入正确的手机号' }],

        // labelWidth:'150',
        // style: {'display':'block','width': 'auto','margin-left':'100px'},
      }]
      // 个人/公司
      let part3 = [{
        type: 'INPUT',
        label: '开票电话',
        fieldName: 'phone',
        placeholder: '',
        initialValue: '',
        rules: [{ required: true, message: '请填写...', trigger: 'blur'}],
      },
      {
        type: 'SELECT',
        label: '证件类型',
        fieldName: 'ctCode',
        placeholder: '',
        initialValue: '',
        itemList: this.createDictList('1508'), // 原'1058'修改为'7035'再改'1508'
        rules: [{ required: true, message: '请选择', trigger: 'blur'}],
        change: val => {
            if(val==15081001){  //身份证
              this.formList.find(item => item.fieldName ==='certificateNo').rules=[{ required: true, trigger: 'blur', validator: IDNumber }]
            } else {
              this.formList.find(item => item.fieldName ==='certificateNo').rules=[{ required: true, message: '请填写...', trigger: 'blur'}]
            }
          }
      },
      {
        type: 'INPUT',
        label: '证件号码',
        fieldName: 'certificateNo' ,
        placeholder: '',
        initialValue: '',
        rules: [{ required: true, message: '请填写...', trigger: 'blur'}],
        change: val => {
          let type = this.formList.find(item => item.fieldName ==='ctCode').initialValue
          if (type == 15081001) {
            const reg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
            if (reg.test(val)){
              let birthday = val.slice(6, 14).replace(/(.{4})(.{2})/, '$1-$2-')
              console.log(moment(birthday), moment(birthday).valueOf())
              this.formList.find(item => item.fieldName ==='birthday').initialValue = moment(birthday).valueOf()
            }
          }
        }
      }]
      // 公司
      let part4 = [{
        type: 'INPUT',
        label: '开票联系人',
        fieldName: 'contactName',
        initialValue: '',
        placeholder: '',
        rules: [{ required: true, message: '请填写...', trigger: 'blur'}],
      },
      {
        type: 'INPUT',
        label: '开票联系人手机',
        fieldName: 'mobilePhone',
        labelWidth:'150',
        style: {'display':'block','width': 'auto','margin-left':'110px'},
        rules: [{ required: true, message: '请填写...', trigger: 'blur'}],
        initialValue: '',
        placeholder: ''
      }]
      // 个人/公司
      let part5 = [{
        type: 'SELECT',
        label: '性别',
        fieldName: 'gender',
        placeholder: '',
        initialValue: '',
        itemList: this.createDictList('1002')
      },
      {
        type: 'DATE',
        label: '生日',
        fieldName: 'birthday',
        placeholder: '',
        initialValue: '',
      },
      {
        type: 'SELECT',
        label: '行业类型',
        fieldName: 'industry',
        placeholder: '',
        initialValue: '',
        itemList: this.createDictList('7030')
      },
      {
        type: 'INPUT',
        label: '开票地址',
        fieldName: 'adderss',
        placeholder: '',
        initialValue: '',
        disabled: false,
        rules: [{ required: true, message: '请填写...', trigger: 'blur'}],
      }]
      let resultList = isPersonal ? [...part1, ...part2, ...part3, ...part5] : [...part1, ...part3, ...part4, ...part5]
      //开票信息
      console.log('createFormList',resultList)
      return resultList
    },
    formChangeHandle(val) {
      this.saveData = _.cloneDeep(val);
    },
    saveDialog () {
      let { type, rowData } = this
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData != null) {
        let params = Object.assign({}, this.saveData)
        params['birthday'] = params['birthday'] ? moment(params['birthday']).format('YYYY-MM-DD HH:mm:ss') : ''
        console.log(params)
        if (type == 'add') {
          addCustomerOne(params).then(res => {
            if (res.success) {
              this.closeDialog('refresh')
            }
          })
        } else if (type == 'edit') {
          params['potentialCustomerNo'] = rowData['potentialCustomerNo']
          params['potentialCustomersId'] = rowData['potentialCustomersId']
          updateCustomerOne(params).then(res => {
            if (res.success) {
              this.closeDialog('refresh')
            }
          })
        }
      }
    },
    closeDialog (val) {
      this.$emit('close', val)
    },
    // 日期预处理
    preDateHandler(val, formatCode='YYYY-MM-DD HH:mm:ss') {
      let resultDate = val ? moment(Number(val)).format(formatCode) : val;
      if ((resultDate == 'Invalid date' || resultDate == 'Invalid Date') && val) {
        resultDate = moment(val).format(formatCode)
      }
      return resultDate;
    },
  }
}
</script>

<style lang="less" scoped>

</style>
